

package com.manes.gui;

import jade.gui.GuiEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.scilab.forge.jlatexmath.DefaultTeXFont;
import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXEnvironment;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;

import com.manes.util.OutputTextPane;

import com.manes.agents.UIAgent;
import javax.swing.JCheckBox;
import javax.swing.JMenuItem;

/**
 *
 * @author Administrator
 */
public class UIAgentGUI extends JFrame implements ActionListener {
    private UIAgent UIA;
    private JTextField sendTxtFld;
    private OutputTextPane receivedTextArea = null;
  //  private JTextArea receivedTextArea;
    private JScrollPane receivedScrollPane;

    public UIAgentGUI(UIAgent pa) {
        super();
        UIA = pa;
        setTitle("Welcome to  "+UIA.getLocalName());
	setSize(615,463);
        
        JPanel mainPanel = new JPanel();
        
        JPanel receivedMsgPanel = new JPanel();
        
        receivedTextArea = new OutputTextPane();
      // this.receivedTextArea.setSize(300,200);
        receivedTextArea.setPreferredSize(new Dimension(550, 300));
        receivedScrollPane = new JScrollPane(receivedTextArea);
        receivedMsgPanel.add(receivedScrollPane);
        
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
     
        
        JPanel sendMsgPanel = new JPanel();
        JLabel sendLabel = new JLabel("Enter Equation:");
        sendTxtFld = new JTextField(30);
        
   
        sendMsgPanel.add(sendLabel);
        sendMsgPanel.add(sendTxtFld);
        
        JButton sendBtn = new JButton("Solve");
        sendBtn.setActionCommand("Send");
        sendBtn.addActionListener(this);
       
        JButton recBtn = new JButton("Recomand");
        recBtn.setActionCommand("Recomand");
        recBtn.addActionListener(this);
        
       // JButton methBtn = new JButton("Met");
       // methBtn.setActionCommand("Met");
       // methBtn.addActionListener(this);
        
        JButton cancelBtn = new JButton("Finish");
        cancelBtn.setActionCommand("Finish");
        cancelBtn.addActionListener(this);
        
        JButton helpBtn = new JButton("Help");
        helpBtn.setActionCommand("Help");
        helpBtn.addActionListener(this);
        
      //  JButton plotBtn = new JButton("Plot");
      //  plotBtn.setActionCommand("Plot");
      //  plotBtn.addActionListener(this);
        
      //  JButton paramPlotBtn = new JButton("ParamPlot");
      //  paramPlotBtn.setActionCommand("ParamPlot");
      //  paramPlotBtn.addActionListener(this);
        
        //JButton plot3DBtn = new JButton("Plot3D");
      //  plot3DBtn.setActionCommand("Plot3D");
      //  plot3DBtn.addActionListener(this);
        
        JPanel buttonPanel = new JPanel();
        //buttonPanel.add(helpBtn);     <-- uncomment to enable help button
        buttonPanel.add(sendBtn);
        buttonPanel.add(recBtn);
     //   buttonPanel.add(methBtn);
     // buttonPanel.add(plotBtn);
    //  buttonPanel.add(paramPlotBtn);
    //  buttonPanel.add(plot3DBtn);
        buttonPanel.add(cancelBtn);
        
        mainPanel.add(receivedMsgPanel);
        mainPanel.add(sendMsgPanel);
        mainPanel.add(buttonPanel);
        
       
        
        

        //Welcome message
        
        receivedTextArea.append("===========================================\n"
        		+"MANES alpha v0.3 \n"
        		+"Developed by Iuhasz Gabriel, Tirea Monica\n"
        		+"UVT 2011\n"
        		+"===========================================\n");
        
        
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        
        JMenuBar menuBar_1 = new JMenuBar();
        setJMenuBar(menuBar_1);
        
        JMenu mnFile = new JMenu("File");
        menuBar_1.add(mnFile);
        
        JMenuItem mntmOpen = new JMenuItem("Open");
        mnFile.add(mntmOpen);
        
        JMenu mnMethod = new JMenu("Method");
        menuBar_1.add(mnMethod);
        
        JMenuItem mntAdd = new JMenuItem("Add");
        mnMethod.add(mntAdd);
        mntAdd.setActionCommand("Met");
        mntAdd.addActionListener(this);
        
        JMenu mnEdit = new JMenu("Plot");
        menuBar_1.add(mnEdit);
        
        //JButton plot3DBtn = new JButton("Plot3D");
        JMenuItem plot3DBtn = new JMenuItem("Plot3D");
        mnEdit.add(plot3DBtn);
        plot3DBtn.setActionCommand("Plot3D");
        plot3DBtn.addActionListener(this);
          
        JMenuItem plotBtn = new JMenuItem("Plot");
        mnEdit.add(plotBtn);
        plotBtn.setActionCommand("Plot");
        plotBtn.addActionListener(this);
        
        JMenuItem paramPlotBtn = new JMenuItem("ParamPlot");
        mnEdit.add(paramPlotBtn);
        paramPlotBtn.setActionCommand("ParamPlot");
        paramPlotBtn.addActionListener(this);
        
        JMenu mnPreferences = new JMenu("Preferences");
        menuBar_1.add(mnPreferences);
        
        JMenu mnHelp = new JMenu("Help");
        menuBar_1.add(mnHelp);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Send")) {
            GuiEvent ev = new GuiEvent(null,UIAgent.SEND_EVENT);
	    UIA.postGuiEvent(ev);
        }
        else if(e.getActionCommand().equals("Recomand")){
            GuiEvent ev = new GuiEvent(null,UIAgent.REC_EVENT);
	    UIA.postGuiEvent(ev);
        }
        else if(e.getActionCommand().equals("Plot")){
        	GuiEvent ev = new GuiEvent(null,UIAgent.PLOT_EVENT);
        UIA.postGuiEvent(ev);
        }
        else if(e.getActionCommand().equals("Help")){
        	GuiEvent ev = new GuiEvent(null,UIAgent.HELP_EVENT);
        UIA.postGuiEvent(ev);
        	
        } 
        else if(e.getActionCommand().equals("Finish")){
        	GuiEvent ev = new GuiEvent(null,UIAgent.EXIT);
        UIA.postGuiEvent(ev);
        	
        }else if(e.getActionCommand().equals("Plot3D")){
        	GuiEvent ev = new GuiEvent(null,UIAgent.PLOT3D_EVENT);
            UIA.postGuiEvent(ev);
        }else if (e.getActionCommand().equals("ParamPlot")){
        	GuiEvent ev = new GuiEvent(null,UIAgent.PARAMPLOT_EVENT);
        	UIA.postGuiEvent(ev);
        }else if (e.getActionCommand().equals("Met")){
        	GuiEvent ev = new GuiEvent(null, UIAgent.METH_EVENT);
        	UIA.postGuiEvent(ev);
        }
    }

    public void setReceivedMsg(String receivedMsg) {
    	TeXFormula formula = new TeXFormula(receivedMsg);
		TeXIcon ticon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 15, TeXConstants.UNIT_PIXEL, 80,
				TeXConstants.ALIGN_LEFT);
		
		//uncomment for latex version
		
		//receivedTextArea.addIcon(ticon, 0, true);
    	receivedTextArea.append("Out[]= "+receivedMsg + "\n");
		
    }

    public String getTextToSend() {
        return this.sendTxtFld.getText();
    }
     public void setDimension(int x, int y){
    	this.receivedTextArea.setSize(x, y);
     }
    

}
